
class Module_System 
	
	def initialize()
		self.LoadModules()
	end
	
	
	def LoadModules()
		$modules = ""
		$modules = Array.new
		Dir.foreach("modules") { 
			|x| 
			if (x =~ /(.*)\.e/)
				load 'modules/'+x+'/main.rb'
				$modules << $1
				if (x =~ /(.*)\.e\.I$/) 
					mod = $1
					Thread.new {
						begin
							eval(mod).initialize()
						rescue
						end
					}				
				elsif (x =~ /(.*)\.e$/) 
					mod = $1
					begin
						eval(mod).initialize()
					rescue
					end
				end
			end
		} 
	end
	
	def Gateway()
	
		Thread.new {
			$modules.each do |m|
				Thread.new {
					if ($buffer =~ /:(.*)!(.*)@(.*) PRIVMSG (.*) :(.*)/)
						nick = $1
						ident = $2
						host = $3
						where = $4
						msg = $5
						begin
							eval(m).PRIVMSG(nick, ident, host, where, msg)
						rescue => e
							#Core.MSG(where="#botarena", what=e.message)
						end
					end
				}
				
				
				Thread.new {
					if ($buffer =~ /:(.*)!(.*)@(.*) NOTICE (.*) :(.*)/)
						nick = $1
						ident = $2
						host = $3
						where = $4
						msg = $5
						begin
							eval(m).NOTICE(nick, ident, host, where, msg)
						rescue
						end
					end
				}
				
				
				Thread.new {
					if ($buffer =~ /:(.*)!(.*)@(.*) JOIN :(.*)/)
						nick = $1
						ident = $2
						host = $3
						where = $4
						begin
							eval(m).JOIN(nick, ident, host, where)
						rescue
						end
					end
				}			
				
				
				Thread.new {
					if ($buffer =~ /:(.*)!(.*)@(.*) PART (.*) :(.*)/)
						nick = $1
						ident = $2
						host = $3
						where = $4
						msg = $5
						begin
							eval(m).PART(nick, ident, host, where, msg)
						rescue
						end
					end
				}			


				Thread.new {
					if ($bufferbits[1] == 376)
						begin
							eval(m).CONNECT()
						rescue 
						end
					end
				}	



			if ($buffer =~ /!rehash/)
				self.LoadModules()
			end

				
				
			
			
			
				Thread.new {
					eval(m).Buffer()
				}
			
			end
		}

	end
	
end